import java.util.Locale;
import java.util.Objects;

public class String_1 {
    public static void main1(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = s1;


        System.out.println("s1和s2引用同一个对象?"+(s1==s2));
        System.out.println("s1和s3引用同一个对象?"+(s1==s3));

    }
    public static void main2(String[] args) {
        String s1 = new String("World");
        String s2 = new String("world");
        String s3 = s1;

        System.out.println("s1等于s2?"+s1.equals(s2));
        System.out.println("s1等于s3?"+ Objects.equals(s1,s3));
    }
    public static void main3(String[] args) {
        String s1 = new String("world");
        String s2 = new String("apple");

        if(s1.compareTo(s2)>0) {
            System.out.println("s1>s2");
        }else if(s1.compareTo(s2)<0){
            System.out.println("s1<s2");
        }else{
            System.out.println("s1==s2");
        }
    }
    public static void main4(String[] args) {
        String s1 = new String("apple");
        String s2 = new String("Apple");

        System.out.println("equalsIgnoreCase::s1==s2?"+s1.equalsIgnoreCase(s2));
        System.out.println("==========");

        if(s1.compareToIgnoreCase(s2)>0) {
            System.out.println("compareToIgnoreCase::s1>s2");
        }else if(s1.compareToIgnoreCase(s2)<0){
            System.out.println("compareToIgnoreCase::s1<s2");
        }else{
            System.out.println("compareToIgnoreCase::s1==s2");
        }
    }

    public static void main5(String[] args) {
        String s = "aabbbcccbbb";

        //1、char charAt(int index)
        System.out.println(s.charAt(2));//b

        //2、int indexOf(int ch)  //将字符转换为ASCII码再运算
        System.out.println(s.indexOf('b'));//2

        //3、int indexOf(char ch, int fromIndex) //从fromIndex下标开始从前往后找
        System.out.println(s.indexOf('b', 5));//8

        //4、int lastIndexOf(int ch)
        System.out.println(s.lastIndexOf('c'));//7

        //5、int lastIndexOf(int ch, int fromIndex)
        System.out.println(s.lastIndexOf('b', 7));//4

    }

    public static void main6(String[] args) {
        String s = "aabbcccbbb";

        //1、int indexOf(String str)
        System.out.println(s.indexOf("bb"));//2

        //2、int indexOf(String str, fromIndex)
        System.out.println(s.indexOf("bb", 4));//7

        //3、int lastIndexOf(String str)
        System.out.println(s.lastIndexOf("cc"));//5

        //4、int lastIndexOf(String str, formIndex)
        // 从fromIndex从后往前向前找，
        // 第一个字母符合后向后接着比较剩下的字母，若剩下的也相等，则返回子串第一个字母在母串中的下标，
        // 否则继续查找，如果遍历完都没有就返回-1
        System.out.println(s.lastIndexOf("bb", 3));//2
    }


    public static void main7(String[] args) {
        //1、数字转换为字符串
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        //这个相当于toString
        String s4 = String.valueOf(new Student("Hanmeimei", 18));
        System.out.println(s1+"\n"+s2+"\n"+s3+"\n"+s4);
        System.out.println("===================");

        //2、字符串转换为数字
        int a = Integer.parseInt(s1);
        double d = Double.parseDouble(s2);
        boolean b = Boolean.parseBoolean(s3);
        System.out.println(a+"\n"+d+"\n"+b);
        //Student类型转换为字符串后，不能再次转换为Student类型。

    }

    public static void main8(String[] args) {
        String s1 = "help";
        String s2 = "HELLO";

        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase());
    }

    public static void main9(String[] args) {
        String s = "hello";
        char[] chars = s.toCharArray();
        //1、字符串=》数组
        for (int i=0;i<s.length();i++){ //s是字符串length后面加()，而chars是数组，求长度不加括号
            System.out.print(chars[i]);
        }
        System.out.println("\n=============");

        //2、数组=》字符串
        String s1 = new String(chars);
        System.out.println(s1);
    }

    public static void main10(String[] args) {
        String s = String.format("%d-%d-%d",2022,9,9);
        System.out.println(s);
    }

    public static void main11(String[] args) {
        String s = "HelloWorld";
        String s1 = s.replaceAll("l","x");
        System.out.println(s1);

        String s3 = s.replaceFirst("l","x");
        System.out.println(s3);
    }

    public static void main12(String[] args) {
        String s1 = "My name is LiHua";
        //1、String[] strings = 字符串.split("分隔符")
        String[] strings = s1.split(" ");
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }

        System.out.println("===================");
        //2、String[] strings = 字符串.split("分隔符, 分成字符串成员个数")
        String[] strings1 = s1.split(" ",3);
        for (int i = 0; i < strings1.length; i++) {
            System.out.println(strings1[i]);
        }
    }

    public static void main13(String[] args) {
        //1、用"\\符号"分割"."，"+"，"*"，"|"等符号
        String s1 = "192.169.1.1";
        String[] strings1 = s1.split("\\.");
        for (int i = 0; i < strings1.length; i++) {
            System.out.println(strings1[i]);
        }

        System.out.println("===================");
        //2、"\\\\"分割"\\"
        String s2 = "abc\\bde|hf+mb"; //第一个\转义第二个\使第二个成为字符
        String[] strings2 = s2.split("\\\\");
        for (int i = 0; i < strings2.length; i++) {
            System.out.println(strings2[i]);
        }

        System.out.println("===================");
        //3、多重分割用"|"
        String s3 = "zhang=San&wang=wu";
        String[] strings3 = s3.split("=|&");
        for (int i = 0; i < strings3.length; i++) {
            System.out.println(strings3[i]);
        }
    }

    public static void main(String[] args) {
        //1、字符串.subString(int beginIndex)
        String s1 = "hello world";
        String cs1 = s1.substring(1);
        System.out.println(cs1);

        //2、字符串.subString(int beginIndex, int endIndex)
        //其中截取片段不包括endIndex，遵循左闭右开
        String cs2 = s1.substring(1,4);
        System.out.println(cs2);
    }
}


//main7
class Student {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}